package com.sdjz.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sdjz.domain.Student;
import com.sdjz.service.StudentService;

/**
 * 
 * @author zhangpeng
 * 
 */

@Controller
@RequestMapping("/test01")
public class StudentController {
	
	@Autowired
	private StudentService studentService;
	
	@RequestMapping("/register")
	public String login(ModelMap modelMap){
		modelMap.addAttribute("student", new Student());
		return "register";
	}
	
	@RequestMapping("/display")
	public ModelAndView displayStudentinfo(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("display");
		mav.addObject("studentList",studentService.findStudentByAll());
		return mav;
	}
	
	
	@RequestMapping("/findById/{id}")
	public ModelAndView findStudentById(@PathVariable("id") Integer id){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/test01/update");
		mav.addObject("student",studentService.findStudentById(id));
		return mav;
	}
	
	@RequestMapping("/addStudent")
	public String addStudent(@Validated @ModelAttribute("student") Student student,BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return "register";
		}else{
			studentService.addStudent(student);
		return "redirect:display.html";
		}
	}
	
	@RequestMapping("/update")
	public String updateStudent(Student student){
		studentService.updateStudent(student);
		return "redirect:display.html";
	}
	
	@RequestMapping("/deleteStudent/{id}")
	public String deleteStudent(Student student){
		studentService.deleteStudent(student);
		return "redirect:/test01/display.html";
	}
	
	@RequestMapping("/toBeExcel")
	public String toExcel(ModelMap modelMap){
		modelMap.addAttribute("studentList", studentService.findStudentByAll());
		return "studentExcel";
	}
}










